//
//  DownMVViewController.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 周莹. All rights reserved.
//

#import "DownMVViewController.h"
#import "Colours.h"
#import "MVDownloadManager.h"
#import "DownloadMVTableViewCell.h"
#define k_newVideoDidStartDown  @"newVideoDidStartDown"

#define k_videoDidDownedFinishedSuccess @"videoDidDownedSuccess"


@interface DownMVViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource, MVDownloadManagerDelegate, DownloadMVTableViewCellDelegate>
@property (strong, nonatomic) IBOutlet UISegmentedControl *segController;
@property (strong, nonatomic) IBOutlet UIScrollView *mainScrollView;
@property (strong, nonatomic) IBOutlet UITableView *downLoadIng;
@property (strong, nonatomic) IBOutlet UITableView *downLoadFinish;

@end

@implementation DownMVViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"下载MV";
    self.view.backgroundColor = [[UIColor alloc] initWithRed:255.0/255 green:222.0/255 blue:249.0/255 alpha:1.0];
    //左按钮
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 30, 30);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"23.png"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    //设置代理
    [MVDownloadManager sharedInstance].delegate = self;
    self.downLoadIng.delegate = self;
    self.downLoadIng.dataSource = self;
    self.mainScrollView.delegate = self;
    
    //注册cell
    [self.downLoadIng registerNib:[UINib nibWithNibName:@"DownloadMVTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    
    //选择栏
    self.segController.tintColor = [UIColor purpleColor];
    self.segController.selectedSegmentIndex = 0;
    //绑定事件
    [self.segController addTarget:self action:@selector(segControllerAction:) forControlEvents:UIControlEventValueChanged];
    //注册新的下载通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMVDidStartDownAction:) name:k_newVideoDidStartDown object:nil];
    //注册下载完成的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MVDidFinishrdSuccess:) name:k_videoDidDownedFinishedSuccess object:nil];
    
}
#pragma mark - 左button事件
-(void)leftButtonAction:(UIButton *)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

#pragma mark - 选择栏点击跳转页面事件
-(void)segControllerAction:(UISegmentedControl *)sender{
    CGFloat x = (sender.selectedSegmentIndex) * self.view.frame.size.width;
    [self.mainScrollView setContentOffset:CGPointMake(x, 0) animated:YES];
}

#pragma mark - 实现换页面就换选择栏下标事件(代理方法)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger page = scrollView.contentOffset.x / self.view.frame.size.width;
    self.segController.selectedSegmentIndex = page;
}
#pragma mark - tableview的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [MVDownloadManager sharedInstance].downMVArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DownloadMVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MVModel *model = [MVDownloadManager sharedInstance].downMVArray[indexPath.row];
    [cell setCellDataWithModel:model];
    cell.delegate = self;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
#pragma mark - 代理----更新下载

#pragma mark 代理
#warning 这里不走 以下 ！！！！！！！！！！！

-(void)MVDidUpDatedProgressWithMVModel:(MVModel *)model{
    NSMutableArray *array = [MVDownloadManager sharedInstance].downMVArray;
    if ([array containsObject:model]) {
        NSInteger index = [array indexOfObject:model];
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
        DownloadMVTableViewCell *cell = (DownloadMVTableViewCell *)[self.downLoadIng cellForRowAtIndexPath:indexPath];
        //判断当前ell是否在显示
        if ([[self.downLoadIng visibleCells] containsObject:cell]) {
            [cell updateDownLoadProgress:model];
        }
    }
}
#pragma mark - 新的电影开始下载的通知
-(void)newMVDidStartDownAction:(NSNotification *)sender{
    [self.downLoadIng reloadData];
}
#pragma mark - mv下载完成
-(void)MVDidFinishrdSuccess:(NSNotification *)sender{
    MVModel *model = sender.object;
    NSInteger index = [[MVDownloadManager sharedInstance].downMVArray indexOfObject:model];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    DownloadMVTableViewCell *cell = (DownloadMVTableViewCell *)[self.downLoadIng cellForRowAtIndexPath:indexPath];
    //设置cell上的下载按钮为完成状态
    cell.palseAndPlayButton.selected = NO;
    [cell.palseAndPlayButton setImage:[UIImage imageNamed:@"37.png"] forState:UIControlStateNormal];
    cell.palseAndPlayButton.userInteractionEnabled = NO;
}

#pragma mark - 代理-----断点下载
-(void)DownloadMVTableViewCellpalseAndPlayButtonDidClicked:(DownloadMVTableViewCell *)cell withIsPlay:(BOOL)isPlay{
    NSIndexPath *indexPath = [self.downLoadIng indexPathForCell:cell];
    MVModel *model = [MVDownloadManager sharedInstance].downMVArray[indexPath.row];
    if (isPlay) {
        [[MVDownloadManager sharedInstance] pausedownloadMVWithMVModel:model];
    } else {
        [[MVDownloadManager sharedInstance] resumedownloadMVWithModel:model];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
